
class Cret{
    constructor(img, left, right,arr) {
        this.img = img;
        this.left = left;
        this.right = right;
        this.arr = arr;
        //核心内容
        this.index = 0;
        this.img.style.backgroundImage = `url(${this.arr[this.index]})`;
        this.time = null
        this.eventBind();
}
    //更换背景图
    setimg() {
        this.img.style.backgroundImage = `url(${this.arr[this.index]})`;
    }
    //右点击
    setright(btn) {
        this.index++
        if (this.index == 2) {
            this.index=0
        }
    
        this.setimg()
    }
    //左点击
    setleft(btn) {
        this.index--;
        if (this.index < 0) {
            this.index=1
        }
    
        this.setimg()
    }

    //自动轮播
    settime() {
        let that=this
       this.time = setInterval(function () {
           that.setright()
        },2000)
    }


    eventBind() {
        let that = this;
        this.right.onclick = function () {
            that.setright(this)
        }
        this.left.onclick = function () {
            that.setleft(this)
        }
       
        this.settime();
        this.img.onmouseover = function () {
            
            clearInterval(that.time)
        }
        this.img.onmouseout= function () {
            that.settime()
        }
    }
} 